package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.Set;

public class PersonalCenterPage extends Utils {
    private static String url = "http://8.134.99.135:58080/index.html";

    public PersonalCenterPage() {
        super(url);
    }

    //未登录状态，直接跳回登录页面
    public void noLogin() {
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.134.99.135:58080/sign-in.html", currentUrl);
    }

    //登陆状态
    public void loginSuc() {
        //获取首页显示的用户名称
        String name = driver.findElement(By.cssSelector("#index_nav_nickname")).getText();
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light." +
                "d-print-none > div > div > div.nav-item.dropdown > a")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();

        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bit-forum-content > div.page-body > div " +
                "> div > div > div.col.d-flex.flex-column > div > div.row.align-items-center > div:nth-child(2)")));
        //检查页面元素是否与预期一样
        String title = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none" +
                " > div > div > div > h2")).getText();
        Assertions.assertEquals("用户中心",title);
        String title1 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col-3.d-none.d-md-block.border-end > div > div > a")).getText();
        Assertions.assertEquals("我的账户",title1);
        //用户名
       driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div >" +
                " div.col.d-flex.flex-column > div  > h2"));

        //头像
        driver.findElement(By.cssSelector("#settings_avatar"));
        String headPortrait = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div >div.col.d-flex.flex-column > div > div.row.align-items-center > div:nth-child(2) > a")).getText();
        Assertions.assertEquals("修改头像",headPortrait);

        //昵称
        String nickname = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > h3:nth-child(4)")).getText();
        Assertions.assertEquals("昵称",nickname);
        String nicknameText = driver.findElement(By.cssSelector("#setting_input_nickname")).getText();
        Assertions.assertEquals(name,nicknameText);
        String nicknameSubmit = driver.findElement(By.cssSelector("#setting_submit_nickname")).getText();
        Assertions.assertEquals("修 改",nicknameSubmit);

        //邮箱地址
        String email = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > h3:nth-child(7)")).getText();
        Assertions.assertEquals("邮箱地址",email);
        String emailSubmit = driver.findElement(By.cssSelector("#setting_submit_email")).getText();
        Assertions.assertEquals("修 改",emailSubmit);

        //电话号码
        String phone = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > h3:nth-child(10)")).getText();
        Assertions.assertEquals("电话号码",phone);
        String phoneSubmit = driver.findElement(By.cssSelector("#setting_submit_phoneNum")).getText();
        Assertions.assertEquals("修 改",phoneSubmit);

        //修改密码
        String modificationPwd = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > h3:nth-child(13)")).getText();
        Assertions.assertEquals("修改密码",modificationPwd);

        //原密码
        String oldPwd = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body >div >div>div>div.col." +
                "d-flex.flex-column > div > div:nth-child(14) > div > div > form > div:nth-child(1) > label")).getText();
        Assertions.assertEquals("原密码",oldPwd);
        String oldPwdText = driver.findElement(By.cssSelector("#settings_input_oldPassword"))
                .getAttribute("placeholder");
        Assertions.assertEquals("请输入密码",oldPwdText);

        //新原密码
        String newPwd = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div >" +
                " form > div:nth-child(2) > label")).getText();
        Assertions.assertEquals("新原密码",newPwd);
        String newPwdText = driver.findElement(By.cssSelector("#settings_input_newPassword"))
                .getAttribute("placeholder");
        Assertions.assertEquals("请输入密码",newPwdText);

        //确认密码
        String affirmPwd = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > div:nth-child(14) >" +
                " div > div > form > div:nth-child(3) > label")).getText();
        Assertions.assertEquals("确认密码",affirmPwd);
        String affirmPwdText = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"))
                .getAttribute("placeholder");
        Assertions.assertEquals("请输入密码",affirmPwdText);

        //提交修改按钮
        String submit = driver.findElement(By.cssSelector("#settings_submit_password")).getText();
        Assertions.assertEquals("提交修改",submit);

        //个人简介
        String introduce = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > " +
                "div.col.d-flex.flex-column > div > h3:nth-child(16)")).getText();
        Assertions.assertEquals("个人简介",introduce);
        driver.findElement(By.cssSelector("#settings_textarea_remark"));
        String introduceSubmit = driver.findElement(By.cssSelector("#settings_submit_remark")).getText();
        Assertions.assertEquals("修 改",introduceSubmit);

    }

    //能否正确修改信息
    //1、昵称
    public void modificationNick(){
        WebElement nickElement = driver.findElement(By.cssSelector("#setting_input_nickname"));
        nickElement.clear();
        nickElement.sendKeys("test666");
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        String succeed = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = succeed.split("\n");
        Assertions.assertEquals("操作成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div " +
                "> span.close-jq-toast-single")).click();
    }

    //2、邮箱地址
    public void modificationEmail(){
        WebElement nickElement = driver.findElement(By.cssSelector("#setting_input_email"));
        nickElement.sendKeys("1685957971@qq.com");
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        String succeed = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = succeed.split("\n");
        Assertions.assertEquals("操作成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div " +
                "> span.close-jq-toast-single")).click();
    }

    //3、电话号码
    public void modificationPhoneNumber(){
        WebElement nickElement = driver.findElement(By.cssSelector("#setting_input_phoneNum"));
        nickElement.sendKeys("1508099396");
        WebElement clickElement = driver.findElement(By.cssSelector("#setting_submit_phoneNum"));

        //按钮被遮挡
        Actions action = new Actions(driver);
        action.moveToElement(clickElement).click().perform();

        String succeed = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = succeed.split("\n");
        Assertions.assertEquals("操作成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div " +
                "> span.close-jq-toast-single")).click();
    }

    //4、密码
    public void modificationPassword(){
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("test1");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("test");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("test");

        WebElement submitElement = driver.findElement(By.cssSelector("#settings_submit_password"));

        //按钮被遮挡
        Actions action = new Actions(driver);
        action.moveToElement(submitElement).click().perform();

        //密码修改成功则跳转到登陆页面
        String currentHandle = driver.getWindowHandle();
        Set<String> windowHandles = driver.getWindowHandles();
        for (String handle : windowHandles){
            if (!handle.equals(currentHandle)){
                currentHandle = handle;
            }
        }
        String currentUrl = driver.getCurrentUrl();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#signInForm")));
        String login = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > " +
                "div.card.card-md > div > h2")).getText();
        Assertions.assertEquals("用户登录",login);
    }


    //5、个人简介
    public void modificationIntroduce(){
        WebElement nickElement = driver.findElement(By.cssSelector("#settings_textarea_remark"));
        nickElement.sendKeys("一步一个脚印");
        WebElement clickElement = driver.findElement(By.cssSelector("#settings_submit_remark"));

        //按钮被遮挡
        Actions action = new Actions(driver);
        action.moveToElement(clickElement).click().perform();

        String succeed = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = succeed.split("\n");
        Assertions.assertEquals("操作成功",s[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div " +
                "> span.close-jq-toast-single")).click();
    }

}
